package ar.fi.uba.fallas2.data;

import java.sql.*;

import ar.fi.uba.fallas2.helpers.BundleReader;

public class Database {
	private static Database instance = null;
	private Connection connection;
	
	public static Database getInstance() {
		if (instance == null) {
			instance = new Database();
		}
		return instance;
	}
	
	private Database() {
		try {
			Class.forName("org.sqlite.JDBC");
			/**
			 * @TODO: cambiar hardcode. ponerlo en las properties grales
			 * */
        	this.setConnection(DriverManager.getConnection("jdbc:sqlite:" + BundleReader.getProperty("db.source")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void close() {
		try {
			this.getConnection().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public PreparedStatement prepareStatement(String queryStatement) throws SQLException {
		return this.getConnection().prepareStatement(queryStatement);
	}

	protected void setConnection(Connection connection) {
		this.connection = connection;
	}

	protected Connection getConnection() {
		return connection;
	}

	public static void getCloseInstance() {
		instance.close();
		instance=null;
	}
}
